let activeEffect:any;

// 实现effect副作用域函数
export const effect = (fn:Function) => {
  const _effect = function () {
    activeEffect = _effect
    fn()
  }

  _effect()
}

const targetMap = new WeakMap()

export const track = (target: object, key: string) => {
  let depsMap = targetMap.get(target)

  if(!depsMap) {
    depsMap = new Map()
    targetMap.set(target, depsMap)
  }

  let deps = depsMap.get(key)

  if(!deps) {
    deps = new Set()
    depsMap.set(key, deps)
  }

  deps.add(activeEffect)
}

// 更新
export const trigger = (target:object, key:string) => {
  const depsMap = targetMap.get(target)
  const deps = depsMap.get(key)

  deps.forEach((effect:any) => effect())
}